package com.yiwei.zxp.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.UploadImgBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.event.SettingShopEvent;
import com.yiwei.zxp.util.ImageUtil;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.V19FrameLayout;
import com.yiwei.zxp.view.uploadImage.UploadImgAdapter;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ShopFirstPhotoActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_retake)
    TextView btnRetake;
    @BindView(R.id.layout_no_take)
    LinearLayout layoutNoTake;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private UploadImgAdapter uploadImgAdapter;
    private Unbinder unbiner;
    private String imgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_first_photo);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("店铺首页");
        String image_url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(image_url)) {
            layoutNoTake.setVisibility(View.GONE);
            btnRetake.setVisibility(View.VISIBLE);
            btnSubmit.setSelected(true);
            imgUrl = image_url;
            Glide.with(this).load(image_url).into(imgPhoto);
        }
    }

    public void getUploadImg(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        UploadImgBean bean = new UploadImgBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.getUploadImg(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    layoutNoTake.setVisibility(View.GONE);
                    btnRetake.setVisibility(View.VISIBLE);
                    btnSubmit.setSelected(true);
                    imgUrl = result.datas.url;
                    Glide.with(this).load(result.datas.url).into(imgPhoto);

                });
    }

    private void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImg(file);
                }
            }

            @Override
            public void callTake(File file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImg(ImageUtil.decodeUri(ShopFirstPhotoActivity.this, Uri.parse(file.getAbsolutePath())));
                }
            }
        }, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_retake, R.id.layout_no_take, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_retake:
                goToTakePhoto();
                break;
            case R.id.layout_no_take:
                goToTakePhoto();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(imgUrl)) {
                    ToastUtil.makeText(this, "门店首页照片不能为空");
                    return;
                }
                SettingShopEvent event = new SettingShopEvent();
                event.event_type = Const.SettingShopType.First_Shop;
                event.shop_img = imgUrl;
                EventBus.getDefault().post(event);
                finish();
                break;
        }
    }
}
